package net.neiquan.applibrary.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.sendtion.xrichtext.GlideApp;

import net.neiquan.applibrary.R;

import java.io.ByteArrayOutputStream;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

public class GlideUtil {

    public static void displayImg(String imgPath, View view) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .priority(Priority.HIGH);
        Glide.with(view.getContext()).load(imgPath).apply(options).into((ImageView) view);
    }

    public static void displayImg(Context context, int imgPath, View view, int defaultImg) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .priority(Priority.HIGH);
        Glide.with(context).load(imgPath).apply(options).into((ImageView) view);
    }

    public static void displayRoundImg(Context context, String imgPath, View view, int defaultImg) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .priority(Priority.HIGH);
        Glide.with(context).load(imgPath).apply(options.bitmapTransform(new CropCircleTransformation(context))).transition(withCrossFade(1000)).into((ImageView) view);
    }

    public static void displayBitmapRoundImg(Context context, Bitmap bitmap, View view, int defaultImg) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.default_img)
                .error(R.drawable.default_img)
                .priority(Priority.HIGH);
        ByteArrayOutputStream baos =new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        Glide.with(context).load(bytes).apply(options.bitmapTransform(new CropCircleTransformation(context))).transition(withCrossFade(1000)).into((ImageView) view);
    }

}
